package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zj.com.adapter.StcjListAdapter;
import zj.com.adapter.ZlzpListAdapter;
import zj.com.db.DBUtil;
import zj.com.model.GjgxDB;
import zj.com.model.JcxDB;
import zj.com.model.StcjDB;
import zj.com.model.UploadFile;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/8.
 * 待确认施工工序 验收
 */
public class StcjListActivity extends BaseActivity implements View.OnClickListener{

    public TextView title_name,right_tv,cancel_tv,submit_tv,del_tv;
    public ImageView left_iv;
    public ListView listView;
    public StcjListAdapter adapter;
    public List<StcjDB> list;

    public Intent intent;

    public LinearLayout bottom_ll;
    public TextView add_tc;
    public RelativeLayout error_rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dqr_sggx);
        initView();

    }

    public void initView(){
        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        add_tc = (TextView) findViewById(R.id.add_tc);
        add_tc.setOnClickListener(this);

        del_tv = (TextView) findViewById(R.id.del_tv);
        del_tv.setOnClickListener(this);

        cancel_tv = (TextView) findViewById(R.id.cancel_tv);
        cancel_tv.setOnClickListener(this);
        bottom_ll = (LinearLayout) findViewById(R.id.bottom_ll);
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        submit_tv.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("实体采集上传");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("编辑");
        add_tc.setText("+ 实体采集情况");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
        listView = (ListView) findViewById(R.id.listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent = new Intent(StcjListActivity.this,StcjDetailActivity.class);
                intent.putExtra("stcjdb",list.get(position));
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);
                adapter.setSelect(position);

                return false;
            }
        });
        list = new ArrayList<StcjDB>();
        adapter = new StcjListAdapter(list,this);
        listView.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    public void initData() {
        try {
       List<StcjDB>   dbs  = DBUtil.getInstance(this).getDBManager().findAll(StcjDB.class);
          if(dbs != null){
              right_tv.setVisibility(View.VISIBLE);
              list = dbs;
              adapter.setDataChange(list);

              if(dbs.size() == 0){
                  right_tv.setVisibility(View.GONE);
                  error_rl.setVisibility(View.VISIBLE);
              }else{
                  error_rl.setVisibility(View.GONE);

              }

          }else{
              right_tv.setVisibility(View.GONE);
          }
        }catch(Exception e) {

        }

        //obtainData();
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.cancel_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                adapter.getSelectMap().clear();
                adapter.notifyDataSetChanged();
                adapter.setSelect(-1);
                break;
            case R.id.submit_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                doServer();
                break;

            case R.id.right_tv:
                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);

                break;
            case R.id.del_tv:
                doDel();
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;
            case R.id.add_tc:
                intent = new Intent(this,StcjAddlActivity.class);
                intent.putExtra("isAdd",true);
                startActivity(intent);
                break;
        }
    }



    public void doDel(){
       Map<Integer,Boolean> selectMap =  adapter.getSelectMap();
        for(Map.Entry<Integer, Boolean> entry:selectMap.entrySet()) {

            int index = entry.getKey();
            if(index >=0 && index <list.size()){
                StcjDB db =  list.get(entry.getKey());
                try{
                    DBUtil.getInstance(this).getDBManager().delete(db);
                    initData();
                }catch(Exception e){

                }
            }


        }
    }

    public ProgressDialog dialog;
    public void doServer(){

        dialog  = DialogUtil.createProgressDialog(this, "提示", "数据上传中...");

        Map<Integer,Boolean> selectMap =  adapter.getSelectMap();
        for(Map.Entry<Integer, Boolean> entry:selectMap.entrySet()) {

          final  StcjDB db = list.get(entry.getKey());
            try {
                NetManager.getInstance(this).newUploadImg(db.getAttachList(), new OnResponseListener<List<UploadFile>>() {
                    @Override
                    public void onComplete(boolean isOK, List<UploadFile> result, int errorCode, String errorMsg) {


                        if (isOK) {

                            doSubmit(db,result);

                        }

                    }

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        dialog.dismiss();
                        Toast.makeText(StcjListActivity.this, "上传图片失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }  catch(Exception e){

            }
        }


    }

    public void doSubmit(final StcjDB db,List<UploadFile> result){

        String uid = (String) SharedPreferencesUtil.getData(this, "uid", "");
        JSONObject obj = new JSONObject();

        JSONArray array = new JSONArray();
        try {
            for(int i=0;i<result.size();i++){
                array.put(i,result.get(i).getResourceId());
            }


            obj.put("UserID", uid);
            if(db.getOrgCode() != null){
                obj.put("orgcode",db.getOrgCode());
                obj.put("projectid",db.getProjectCode());

            }else{
                obj.put("InfoId",db.getInfoId());

            }
            obj.put("InfoRemark",db.getInfoRemark());
            obj.put("InfoAttachList", array);
            obj.put("InfoName",db.getInfoName());


        }catch(Exception e){

            LogUtils.getInstance().error(e.toString());
        }
        NetManager.getInstance(this).SaveEntityProject(obj.toString(), new OnResponseListener<List<JcxDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcxDB> result, int errorCode, String errorMsg) {

                dialog.dismiss();
                if (isOK) {
                    try {
                        DBUtil.getInstance(StcjListActivity.this).getDBManager().delete(db);
                        initData();
                    } catch (Exception e) {

                    }
                    Toast.makeText(StcjListActivity.this, " 提交成功", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(StcjListActivity.this, " 提交失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(StcjListActivity.this, " 提交失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });
    }
}
